{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "173939c0",
   "metadata": {},
   "source": [
    "### P073 关联规则 - 购物篮数据拆分"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "c919cf19",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e1721055",
   "metadata": {},
   "outputs": [],
   "source": [
    "data = {'products': ['bread eggs', 'bread eggs milk', 'milk cheese', \n",
    "                     'bread butter cheese', 'eggs milk', \n",
    "                     'bread milk butter cheese']}\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cf6fc347",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'products': ['bread eggs',\n",
       "  'bread eggs milk',\n",
       "  'milk cheese',\n",
       "  'bread butter cheese',\n",
       "  'eggs milk',\n",
       "  'bread milk butter cheese']}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ed2e825c",
   "metadata": {},
   "outputs": [],
   "source": [
    "transactions = pd.DataFrame(data=data, index=range(1,7))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f7a7f364",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>products</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bread eggs</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>bread eggs milk</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>milk cheese</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bread butter cheese</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>eggs milk</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>bread milk butter cheese</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                   products\n",
       "1                bread eggs\n",
       "2           bread eggs milk\n",
       "3               milk cheese\n",
       "4       bread butter cheese\n",
       "5                 eggs milk\n",
       "6  bread milk butter cheese"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transactions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "7ad33571",
   "metadata": {},
   "outputs": [],
   "source": [
    "expanded = transactions[\"products\"].str.split(expand=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "76ce8d54",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bread</td>\n",
       "      <td>eggs</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>bread</td>\n",
       "      <td>eggs</td>\n",
       "      <td>milk</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>milk</td>\n",
       "      <td>cheese</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bread</td>\n",
       "      <td>butter</td>\n",
       "      <td>cheese</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>eggs</td>\n",
       "      <td>milk</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>bread</td>\n",
       "      <td>milk</td>\n",
       "      <td>butter</td>\n",
       "      <td>cheese</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       0       1       2       3\n",
       "1  bread    eggs    None    None\n",
       "2  bread    eggs    milk    None\n",
       "3   milk  cheese    None    None\n",
       "4  bread  butter  cheese    None\n",
       "5   eggs    milk    None    None\n",
       "6  bread    milk  butter  cheese"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expanded"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8c35270d",
   "metadata": {},
   "source": [
    "### P074 关联规则 - 计算购买商品的去重列表\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "cedef8b0",
   "metadata": {},
   "outputs": [],
   "source": [
    "products = set()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "fb1248e2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'bread', 'butter', 'cheese', 'eggs', 'milk'}"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for column in expanded.columns:\n",
    "    for product in expanded[column].unique():\n",
    "        if product:\n",
    "            products.add(product)\n",
    "products"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "9989ccfa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bread', 'butter', 'cheese', 'eggs', 'milk']"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products = sorted(list(products))\n",
    "products"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a673c6cc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "1e8a67bd",
   "metadata": {},
   "source": [
    "### P075 关联规则 - 实现one-hot编码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "a9f1222b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>bread</td>\n",
       "      <td>eggs</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>bread</td>\n",
       "      <td>eggs</td>\n",
       "      <td>milk</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>milk</td>\n",
       "      <td>cheese</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>bread</td>\n",
       "      <td>butter</td>\n",
       "      <td>cheese</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>eggs</td>\n",
       "      <td>milk</td>\n",
       "      <td>None</td>\n",
       "      <td>None</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>bread</td>\n",
       "      <td>milk</td>\n",
       "      <td>butter</td>\n",
       "      <td>cheese</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "       0       1       2       3\n",
       "1  bread    eggs    None    None\n",
       "2  bread    eggs    milk    None\n",
       "3   milk  cheese    None    None\n",
       "4  bread  butter  cheese    None\n",
       "5   eggs    milk    None    None\n",
       "6  bread    milk  butter  cheese"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "expanded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "19a9e7c8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bread', 'butter', 'cheese', 'eggs', 'milk']"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "products"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "a262688b",
   "metadata": {},
   "outputs": [],
   "source": [
    "transactions_encoded = np.zeros(\n",
    "    (len(expanded), len(products)), dtype='int8'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "b6ffffaf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0],\n",
       "       [0, 0, 0, 0, 0]], dtype=int8)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transactions_encoded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "5e824019",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 ['bread' 'eggs' None None]\n",
      "1 ['bread' 'eggs' 'milk' None]\n",
      "2 ['milk' 'cheese' None None]\n",
      "3 ['bread' 'butter' 'cheese' None]\n",
      "4 ['eggs' 'milk' None None]\n",
      "5 ['bread' 'milk' 'butter' 'cheese']\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 1, 0],\n",
       "       [1, 0, 0, 1, 1],\n",
       "       [0, 0, 1, 0, 1],\n",
       "       [1, 1, 1, 0, 0],\n",
       "       [0, 0, 0, 1, 1],\n",
       "       [1, 1, 1, 0, 1]], dtype=int8)"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "for row in zip(range(len(expanded)), expanded.values):\n",
    "    print(row[0], row[1])\n",
    "    for idx, product in enumerate(products):\n",
    "        # print(idx, product)\n",
    "        if product in row[1]:\n",
    "            transactions_encoded[row[0], idx] = 1\n",
    "transactions_encoded"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "f1c77781",
   "metadata": {},
   "outputs": [],
   "source": [
    "transactions_encoded_df = pd.DataFrame(\n",
    "    transactions_encoded,\n",
    "    columns=products\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "07459ced",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>bread</th>\n",
       "      <th>butter</th>\n",
       "      <th>cheese</th>\n",
       "      <th>eggs</th>\n",
       "      <th>milk</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   bread  butter  cheese  eggs  milk\n",
       "0      1       0       0     1     0\n",
       "1      1       0       0     1     1\n",
       "2      0       0       1     0     1\n",
       "3      1       1       1     0     0\n",
       "4      0       0       0     1     1\n",
       "5      1       1       1     0     1"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transactions_encoded_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d41a25b7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "6a69d28c",
   "metadata": {},
   "source": [
    "### P076 关联规则挖掘 - 计算商品的支持度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "92be1598",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bread     4\n",
       "butter    2\n",
       "cheese    3\n",
       "eggs      3\n",
       "milk      4\n",
       "dtype: int64"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transactions_encoded_df.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "59a2b50e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "bread     0.666667\n",
       "butter    0.333333\n",
       "cheese    0.500000\n",
       "eggs      0.500000\n",
       "milk      0.666667\n",
       "dtype: float64"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "support = transactions_encoded_df.sum() / len(transactions_encoded_df)\n",
    "support"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fa044b78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39b971d0",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "8446c766",
   "metadata": {},
   "source": [
    "### P077 关联规则挖掘 - 计算多个商品的支持度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "67b1a332",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>bread</th>\n",
       "      <th>butter</th>\n",
       "      <th>cheese</th>\n",
       "      <th>eggs</th>\n",
       "      <th>milk</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   bread  butter  cheese  eggs  milk\n",
       "0      1       0       0     1     0\n",
       "1      1       0       0     1     1\n",
       "2      0       0       1     0     1\n",
       "3      1       1       1     0     0\n",
       "4      0       0       0     1     1\n",
       "5      1       1       1     0     1"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transactions_encoded_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "c1b5708c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.3333333333333333"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sup_butter_bread = (\n",
    "    len(transactions_encoded_df.query(\"butter==1 and bread==1\"))\n",
    "    / \n",
    "    len(transactions_encoded_df)\n",
    ")\n",
    "sup_butter_bread"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "27cb3b0e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.16666666666666666"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sup_butter_milk = (\n",
    "    len(transactions_encoded_df.query(\"butter==1 and milk==1\"))\n",
    "    / \n",
    "    len(transactions_encoded_df)\n",
    ")\n",
    "sup_butter_milk"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a6430113",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "faa1b2c7",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "d00e5a53",
   "metadata": {},
   "source": [
    "### P078 关联规则挖掘 - 计算关联规则以及置信度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "443bde86",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>bread</th>\n",
       "      <th>butter</th>\n",
       "      <th>cheese</th>\n",
       "      <th>eggs</th>\n",
       "      <th>milk</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   bread  butter  cheese  eggs  milk\n",
       "0      1       0       0     1     0\n",
       "1      1       0       0     1     1\n",
       "2      0       0       1     0     1\n",
       "3      1       1       1     0     0\n",
       "4      0       0       0     1     1\n",
       "5      1       1       1     0     1"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "transactions_encoded_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "1eae7e2f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.6666666666666666"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conf_cheese_brand = (\n",
    "    len(transactions_encoded_df.query(\"cheese==1 and bread==1\")) \n",
    "    /\n",
    "    len(transactions_encoded_df.query(\"cheese==1\")) \n",
    ")\n",
    "conf_cheese_brand"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ca8e42a8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.0"
      ]
     },
     "execution_count": 54,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conf_butter_cheese = (\n",
    "    len(transactions_encoded_df.query(\"butter==1 and cheese==1\")) \n",
    "    /\n",
    "    len(transactions_encoded_df.query(\"butter==1\")) \n",
    ")\n",
    "conf_butter_cheese"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2596b78",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c4f68f15",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ab89cc06",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
